package cn.jinjimi.app.main.activity;

import android.content.Intent;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import cn.jinjimi.base.util.CollectionUtils;
import cn.jinjimi.base.util.ToastUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.controller.UserController;
import cn.jinjimi.app.controller.event.DataEvent;
import cn.jinjimi.app.controller.event.ThemeStandEvent;
import cn.jinjimi.app.entity.StandTheme;
import cn.jinjimi.app.plugin.InnerWebPluginManager;
import cn.jinjimi.app.plugin.SimplePluginManager;
import cn.jinjimi.app.main.extend.ToolBarCommonActivity;
import cn.jinjimi.app.main.widget.MyGridView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

/**
 * Created by haner on 15-2-6.
 */
public class ModifyStandThemeActivity extends ToolBarCommonActivity implements AdapterView.OnItemClickListener {

    private static final int MODIFYTHEME = 100;
    private MyGridView myGridView;
    private ImageView advImage;
    private StandTheme standTheme;
    private ArrayList<StandTheme.Theme> themes;
    private int currentSelectedThemeNum = 0;
    private int lastClickItemNum;
    private MAdapter mAdapter;

    @Override
    protected void beforeInitView() {
        setContentView(R.layout.activity_modify_stand_theme);
    }

    @Override
    protected void initial() {
        initView();
        initListener();
        initData();
    }

    protected void initView() {
        super.setTitle(R.string.site_diy);
        advImage = (ImageView) findViewById(R.id.iv_adv);
        myGridView = (MyGridView) findViewById(R.id.gv_themes);
        myGridView.setOnItemClickListener(this);
    }

    protected void initListener() {
        advImage.setOnClickListener(this);
    }

    protected void initData() {
        showLoadingIndicator();
        UserController.getTheme();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onThemeStandEvent(ThemeStandEvent event) {
        hideLoadingIndicator();
        if (isFinishing())
            return;

        switch (event.state) {
            case DataEvent.SUCC:
                standTheme = event.data;
                if (!CollectionUtils.isEmpty(standTheme.getThemes())) {
                    themes = (ArrayList<StandTheme.Theme>) standTheme.getThemes();
                    mAdapter = new MAdapter();
                    myGridView.setAdapter(mAdapter);
                }
                break;
            case DataEvent.FAIL:
                ToastUtils.toastMessage(ModifyStandThemeActivity.this, "网络不给力");
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        lastClickItemNum = position;
        //处理点击某项时的跳转
        Intent intent = new Intent(ModifyStandThemeActivity.this, ActivateThemeActivity.class);
        intent.putExtra("themeName", themes.get(position).getThemeName());
        intent.putExtra("themeFileName", themes.get(position).getThemeFileName());
        intent.putExtra("isVip", themes.get(position).getIsVip());
        intent.putExtra("used", themes.get(position).isUsed());
        intent.putExtra("themeId", themes.get(position).getThemeId());
        intent.putExtra("comment", themes.get(position).getComment());
        startActivityForResult(intent, MODIFYTHEME);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
    }

    class MAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return standTheme.getThemes().size();
        }

        @Override
        public Object getItem(int position) {
            return standTheme.getThemes().get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(ModifyStandThemeActivity.this,
                        R.layout.item_stand_theme, null);
                holder = new ViewHolder();
                holder.imageView = (ImageView) convertView.findViewById(R.id.iv_theme_preview);
                holder.activateBg = convertView.findViewById(R.id.activate_theme);
                holder.title = (TextView) convertView.findViewById(R.id.tv_desc);
                holder.tag = (TextView) convertView.findViewById(R.id.tv_tag);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            //设置内容
            holder.imageView.setImageURI(Uri.parse(themes.get(position).getImageUrl()));
            holder.activateBg.setVisibility(View.INVISIBLE);
            if (themes.get(position).isUsed()) {
                currentSelectedThemeNum = position;
                holder.activateBg.setVisibility(View.VISIBLE);
            }
            holder.title.setText(themes.get(position).getThemeName());
            if (themes.get(position).getIsVip() == 1) {
                holder.tag.setText("诚信通专属");
                holder.tag.setTextColor(ContextCompat.getColor(ModifyStandThemeActivity.this, R.color.orange));
                holder.tag.setBackgroundResource(R.drawable.theme_txt_shape_vip);
            }
            return convertView;
        }

    }

    static class ViewHolder {
        private ImageView imageView;
        private View activateBg;
        private TextView title;
        private TextView tag;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //处理启用返回
        if (resultCode == 200) {
            //通知Adapter改变
            themes.get(lastClickItemNum).setUsed(true);
            themes.get(currentSelectedThemeNum).setUsed(false);
            mAdapter.notifyDataSetChanged();
        }
    }
}
